﻿package com.zmcn.msg.business.msgOrder.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import com.zmcn.msg.bean.vo.UserVO;
import com.zmcn.msg.business.msgOrder.service.MsgOrderService;
import com.zmcn.msg.business.msgOrder.vo.MsgOrderVO;
import com.zmcn.msg.fw.action.BaseAction;
import com.zmcn.msg.fw.action.Page;
import com.zmcn.msg.fw.util.CodeUtil;
import com.zmcn.msg.fw.util.OutUtil;
import com.zmcn.msg.pub.Const;

/**
 * 
 * 消息订单Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/msgOrder")
@Results({ @Result(name = "viewList", location = Const.JSP_msgOrder_list),
	@Result(name = "viewAdd", location = Const.JSP_msgOrder_add),
	@Result(name = "viewEdit", location = Const.JSP_msgOrder_edit),
	@Result(name = "viewFrontMsg", location = Const.JSP_front_msg),
	@Result(name = "frontHistory", location = Const.JSP_front_history)
})
public class MsgOrderAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(MsgOrderAction.class);
	private MsgOrderService service;

	@Autowired
	public void setService(MsgOrderService service) {
		this.service = service;
	}

	/**
	 * 进入消息订单列表页面
	 * 
	 * @return
	 */
	public String viewList() {
		return "viewList";
	}

	

	/**
	 * 获取消息订单列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<MsgOrderVO> page = service.getList(new Page<MsgOrderVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增消息订单
	 * 
	 * @return
	 */
	public String add() {
		try {
			HttpServletRequest req = getRequest();
			String msgId = ServletRequestUtils.getStringParameter(req, "msgId");
			String[] groups = ServletRequestUtils.getStringParameters(req, "groupId");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.add(msgId, groups, userVO.getUserId());
			req.getRequestDispatcher("msg!viewFrontMsg.action").forward(req, getResponse());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return null;
	}
	/**
	 * <p>Title: sendmsg</p>
	 * <p>Description: TODO 消息订单下发</p>
	 * @return
	 * String
	 * @throws
	 * @author 谭永戈
	 * 2012-12-7 下午6:52:31
	 */
	public String sendmsg(){
		Map<String, Object> map = CodeUtil.getJsonMap();
		int count = 0;
		try{
			String[] orderIds = ServletRequestUtils.getStringParameters(getRequest(), "orderId");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			count = service.sendmsg(orderIds, userVO);
			map.put("success", true);
		}catch(Exception e){
			map.put("success", false);
			log.error(e.getMessage(), e);
		}
		map.put("count", count);
		OutUtil.outJson(map, getResponse());
		return null;
	}
	/**
	 * <p>Title: frontHistory</p>
	 * <p>Description: TODO 前端消息订单历史页面</p>
	 * @return
	 * String
	 * @throws
	 * @author 谭永戈
	 * 2012-12-7 下午6:53:27
	 */
	public String frontHistory(){
		return "frontHistory";
	}
}
